﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using JBeamLibrary;
using JBeamLibrary.VehicleParts;

namespace DriveBeamEditor.Forms
{
    public partial class MainForm : Form
    {
        #region Fields

        private Vehicle _selectedVehicle = null;

        #endregion

        #region Properties

        #endregion

        #region CTORs

        public MainForm()
        {
            InitializeComponent();
            InitializeConfig();
        }

        #endregion

        #region Events

        #region Overrides

        protected override void OnShown(EventArgs e)
        {
            Opacity = 0;
            using (var splash = new SplashForm {IdleTime = 1000})
            {
                splash.ShowDialog(this);
            }
            base.OnShown(e);
            Opacity = 1;
        }

        #endregion

        #region Vehicle menu

        private void newVehicleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /* Save current vehicle info */

            /* Create a new vehicle */
            Vehicle vehicle = null;
            using (var dialog = new NewVehicleForm())
            {
                if (dialog.ShowDialog(this) == DialogResult.OK)
                    vehicle = dialog.Vehicle;
            }

            /* No vehicle created, return */
            if (vehicle == null) return;

            /* Create a menu item for the new vehicle */
            var item = CreateVehicleItem(vehicle);

            /* Load new vehicle values */
            item.PerformClick();
        }

        private void openVehicleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /* Choose the vehicle to load */
            string vehicleName = null;
            using (var dialog = new OpenVehicleForm())
            {
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    vehicleName = dialog.SelectedVehicleName;
                }
            }

            /* Operation was cancelled by user */
            if (string.IsNullOrEmpty(vehicleName)) return;

            /* Load the vehicle */
            var vehicle = Vehicle.LoadVehicle(vehicleName);

            /* Display contents */
            var item = CreateVehicleItem(vehicle);
            item.PerformClick();
        }

        private void saveVehicleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /* No vehicle selected */
            if (_selectedVehicle == null) return;

            /* Save the vehicle */
            Vehicle.SaveVehicle(_selectedVehicle);
        }

        private void saveVehicleAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /* No vehicle selected */
            if (_selectedVehicle == null) return;

            /* Choose vehicle name */
            string vehicleName = null;
            using (var dialog = new SaveVehicleForm())
            {
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    vehicleName = dialog.SelectedVehicleName;
                }
            }

            /* Operation was cancelled by user */
            if (string.IsNullOrEmpty(vehicleName)) return;

            /* Save the vehicle */
            Vehicle.SaveVehicleAs(_selectedVehicle, vehicleName);
        }

        /// <summary>Close form</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Tools menu

        /// <summary>Show preferences</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var dialog = new PreferencesForm())
            {
                dialog.ShowDialog(this);
            }
        }

        #endregion

        #region Vehicle parts list

        private void listBoxParts_SelectedIndexChanged(object sender, EventArgs e)
        {
            nodesControl.VehiclePart = listBoxParts.SelectedItem as VehiclePart;
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Read the configuration file
        /// </summary>
        private void InitializeConfig()
        {
            //Config.Read(); (see 'Program.cs')
        }

        private ToolStripItem CreateVehicleItem(Vehicle vehicle)
        {
            var item = new ToolStripButton(vehicle.Name, Properties.Resources.car_icon) { Tag = vehicle };
            item.Click += (s, a) => SelectVehicle(item.Tag as Vehicle);
            vehiclesMenu.Items.Add(item);

            return item;
        }

        private void SelectVehicle(Vehicle vehicle)
        {
            /* Set selected vehicle */
            _selectedVehicle = vehicle;

            /* Check the item and uncheck others */
            foreach (ToolStripButton vItem in vehiclesMenu.Items)
            {
                vItem.Checked = vItem.Tag == vehicle;
            }

            /* Update vehicle info displayed */
            FillVehicleParts(vehicle);

            /* Update hierarchy control */
            vehicleHierarchyControl.Vehicle = vehicle;
        }

        private void FillVehicleParts(Vehicle vehicle)
        {
            listBoxParts.Items.Clear();
            if (vehicle == null) return;

            foreach (var part in vehicle)
            {
                listBoxParts.Items.Add(part);
            }
        }

        #endregion
    }
}
